package stringMVC.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import stringMVC.model.UserInfo;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Controller//返回界面
@RequestMapping("/return")
public class ReturnController {
    @RequestMapping(value = "/r1")
    public String r1(){
        return "/index.html";
    }
    @ResponseBody//返回数据
    @RequestMapping("/r2")
    public String r2(){
        return "123456";
    }
    @ResponseBody//默认是html类型
    @RequestMapping("/r3")
    public String r3(){
        return "<h1>我是html片段</h1>";
    }
    @ResponseBody
    @RequestMapping("/r4")//自动转成json
    public UserInfo r4(){
        UserInfo userInfo = new UserInfo();
        userInfo.setAge(10);
        userInfo.setId(1);
        userInfo.setName("zhang");
        return userInfo;
    }
    @ResponseBody
    @RequestMapping("/r6")//自动转成json
    public Map<String,String> r6(){
        Map<String,String> map = new HashMap<>();
        map.put("k","v");
        return map;
    }
    @RequestMapping("/r7")
    public String r7(){
        return "/a.js";
    }
    @RequestMapping("/r8")
    public String r8(){
        return "/b.css";
    }
    @ResponseBody
    @RequestMapping("/r9")
    public String r9(HttpServletResponse response){
        response.setStatus(404);
        return "设置成功";
    }
    @ResponseBody
    @RequestMapping(value = "/r10",produces = "application/json; charset=utf8")
    public String r10(){
        return "{\"ok\":1}";
    }

    @ResponseBody
    @RequestMapping(value = "/r11")
    public String r11(HttpServletResponse response){
        response.setHeader("a","b");
        return "设置成功";
    }
}
